<?php


namespace App\Tools;


use App\Models\Admin\Area;

class Prarea
{
    /**
     * 获取省
     */
    public function getProvince()
    {
        $info = file_get_contents('http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2019/index.html');
        $result = preg_match_all('#<td.+?>(.+?)</td>#', $info, $arr);
        if ($arr && $arr[0] && $arr[1]) {
            $listA = $arr[0];
            $dataArr = [];
            foreach ($listA as $kk => $vv) {
                preg_match('/<a .*?href=\'(.*?)\'.*?>/is', $vv, $array2);
                if ($array2) {
                    $dataArr[$kk]['url'] = $array2[1];
                    $dataArr[$kk]['code'] = str_replace('.html', '', $array2[1]);
                } else {
                    $dataArr[$kk]['code'] = 0;
                    $dataArr[$kk]['url'] = '';
                }
            }
            $list = $arr[1];
            foreach ($list as $kk => $vv) {
                $dataArr[$kk]['name'] = str_replace('<br/></a>', '', iconv('GB2312', 'UTF-8', $vv));
            }
            unset($dataArr[0]);
            Area::insert($dataArr);
            v($dataArr);
        }
        var_dump($arr);
        exit;
    }

    /**
     * 获取城市数据
     */
    public static function getCity()
    {
//        v(strlen('650100000000'));
//        $info = file_get_contents("http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2019/11.html");
        /*        $result = preg_match_all('#<td.+?>(.+?)</td>#', $info, $arr);*/
//        v($arr);

        $areaList = Area::where(['level' => 2])->get();
        foreach ($areaList as $kk1 => $vv1) {
            $info = file_get_contents("http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2019/{$vv1['url']}");
            if ($info) {
                preg_match_all('#<td.+?>(.+?)</td>#', $info, $arr);
//                v($arr);
                if ($arr && $arr[0] && $arr[1]) {
                    $listA = $arr[0];
                    $dataArr = [];
                    foreach ($listA as $kk => $vv) {
                        preg_match('/<a .*?href=\'(.*?)\'.*?>/is', $vv, $array2);
                        if ($array2) {
                            $name = str_replace('</a>', '', iconv('GB2312', 'UTF-8', $arr[1][$kk]));
                            if (!$dataArr[$array2[1]]['code']) {
                                $dataArr[$array2[1]]['code'] = $name;
                            }
                            if (strlen($name) == 12) {
                                $dataArr[$array2[1]]['code'] = $name;
                            }
                            $dataArr[$array2[1]]['name'] = $name;
                            $dataArr[$array2[1]]['url'] = $array2[1];
                            $dataArr[$array2[1]]['pid'] = $vv1['id'];
                            $dataArr[$array2[1]]['level'] = 3;
                        }
                    }
                    Area::insert(array_values($dataArr));
                }
            }

        }
    }

    /**
     * {name:'北京', cityList:[
     * {name:'市辖区', areaList:['东城区','西城区','崇文区','宣武区','朝阳区','丰台区','石景山区','海淀区','门头沟区','房山区','通州区','顺义区','昌平区','大兴区','怀柔区','平谷区']},
     * {name:'县', areaList:['密云县','延庆县']}
     * ]}
     */
    public static function getJson()
    {
        $list = Area::where(['level' => 1])->get()->toArray();
        $tmp = [];
        foreach ($list as $kk => $vv) {
            $list2 = Area::where(['pid' => $vv['id']])->get()->toArray();
            $tmp2 = [];
            foreach ($list2 as $kk2 => $vv2) {
                $list3 = Area::where(['pid' => $vv2['id']])->get()->toArray();
                $tmp3 = [];
                foreach ($list3 as $vv3) {
                    $tmp3[] = $vv3['name'];
                }
                $tmp2[$kk2]['name'] = $vv2['name'];
                $tmp2[$kk2]['areaList'] = $tmp3;
            }
            $tmp[$kk]['name'] = $vv['name'];
            $tmp[$kk]['cityList'] = $tmp2;
        }

        echo json_encode($tmp, JSON_UNESCAPED_UNICODE);

    }


}
